package com.supermap.mouseListeners;

import com.supermap.Helper;
import com.supermap.MainFrame;
import com.supermap.ui.MapControl;
import com.supermap.ui.SceneControl;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class MainPaneMouseListener implements MouseListener {
    private final JTabbedPane tabbedPane;
    private final JSplitPane treeSplitPane;
    private JPopupMenu popupMenu;

    public MainPaneMouseListener(JTabbedPane tabbedPane, JSplitPane treeSplitPane) {
        this.tabbedPane = tabbedPane;
        this.treeSplitPane = treeSplitPane;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        Object source = e.getSource();
        if (!(source instanceof JTabbedPane)) {
            return;
        }

        if (popupMenu != null) {
            popupMenu.removeAll();
            popupMenu.setVisible(false);
        } else {
            popupMenu = new JPopupMenu();
        }

        switch (e.getButton()) {
            // 左键
            case MouseEvent.BUTTON1:
                break;
            // 中键
            case MouseEvent.BUTTON2:
                closeSelectedTab();
                break;
            // 右键
            case MouseEvent.BUTTON3:
                if (tabbedPane.getSelectedIndex() == -1) {
                    return;
                }

                JMenuItem closeItem = new JMenuItem("关闭");
                closeItem.addActionListener(e1 -> {
                    popupMenu.setVisible(false);
                    closeSelectedTab();
                });
                popupMenu.add(closeItem);
                popupMenu.setLocation(e.getLocationOnScreen());
                popupMenu.setVisible(true);
                break;
            default:
                break;
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

    private void closeSelectedTab() {
        int selectedIndex = tabbedPane.getSelectedIndex();
        Component component = tabbedPane.getComponentAt(selectedIndex);
        tabbedPane.remove(selectedIndex);
        tabbedPane.remove(component);
        if (component instanceof MapControl) {
            MapControl mapControl = (MapControl) component;
            mapControl.getMap().close();
            System.gc();
        } else if (component instanceof SceneControl) {
            SceneControl sceneControl = (SceneControl) component;
            sceneControl.getScene().close();
            System.gc();
        }
        Container parent = tabbedPane.getRootPane().getParent();
        if (parent instanceof MainFrame) {
            MainFrame mainFrame = (MainFrame) parent;
            Component selectedComponent = tabbedPane.getSelectedComponent();
            Helper.updateComponentsEnabled(new Component[]{mainFrame.objectOperateMenu, mainFrame.transportationMenu}, selectedComponent instanceof MapControl);
            Helper.updateComponentsEnabled(new Component[]{mainFrame.styleSettingMenu, mainFrame.space3DAnalystMenu}, selectedComponent instanceof SceneControl);
        }
        Component bottomComponent = treeSplitPane.getBottomComponent();
        if (tabbedPane.getTabCount() == 0 && bottomComponent != null) {
            treeSplitPane.setBottomComponent(null);
        }
    }
}
